#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define maxn 10005
#define inf 0x4fffffff
int n;
struct work
{
    int cost;
};
work works[maxn];
vector<vector<int>> graph;
int MinTime[maxn];
int mintime = -inf;
int main()
{
    cin >> n;
    //构造图
    graph.resize(n + 1);
    for (int i = 0, order, cost, tmp; i < n; ++i)
    {
        cin >> order >> cost;
        works[order].cost = cost;
        do
        {
            cin >> tmp;
            graph[order].push_back(tmp);
        } while (tmp != 0);
    }
    for (int order = 1; order <= n; ++order)
    {
        MinTime[order] = works[order].cost;
        int maxium = 0;
        for (int i = 0, node; i < graph[order].size(); ++i)
        {
            node = graph[order][i];
            maxium = max(maxium, MinTime[node]);
        }
        MinTime[order] += maxium;
        mintime = max(mintime, MinTime[order]);
    }
    cout << mintime << endl;
    return 0;
}